package org.zxyj.web.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.zxyj.core.constants.Constants;
import org.zxyj.core.constants.Keys;
import org.zxyj.core.entity.Result;
import org.zxyj.core.errorno.SystemErrorNo;
import org.zxyj.core.exception.ServiceException;
import org.zxyj.core.exception.UnifyException;
import org.zxyj.core.web.controller.BaseController;
import org.zxyj.system.domain.Resource;
import org.zxyj.system.server.IResourceService;
import org.zxyj.system.type.ResourceTarget;
import org.zxyj.system.type.ResourceType;

/**
 * @author zhaoyong
 * @version 2016年2月19日
 */
@Controller
@RequestMapping("/resource")
public class ResourceController extends BaseController {

	@javax.annotation.Resource
	private IResourceService resourceService;

	@RequestMapping({ "", "/", "/index" })
	public ModelAndView index(HttpServletRequest request, ModelAndView mv, Resource resource) {
		mv.setViewName(Views.INDEX_VIEW);

		Map<String, Object> criteria = new HashMap<String, Object>();
		criteria.put("name", resource.getName());
		criteria.put("parent", resource.getParent());
		List<Resource> resources = resourceService.listEntitiesForBack(criteria);
		mv.addObject(Keys.KEY_PAGE_DATA, resources);
		return mv;
	}

	@RequestMapping("/list/exec")
	@ResponseBody
	public Result<?> doList(HttpServletRequest request, Resource resource) {
		Result<List<Resource>> result = new Result<List<Resource>>();
		try {
			Map<String, Object> criteria = new HashMap<String, Object>();
			criteria.put("name", resource.getName());
			criteria.put("parent", resource.getParent());
			List<Resource> resources = resourceService.listEntitiesForBack(criteria);
			result.setData(resources);

			result.setResultCode(Constants.Common.DEFAULT_RESULT_CODE);
		} catch (ServiceException e) {
			result.setResultCode(e.getErrorNo());
		}
		return result;
	}

	@RequestMapping("/add")
	public ModelAndView add(HttpServletRequest request, ModelAndView mv, Resource resource) {
		mv.setViewName(Views.ADD_VIEW);
		mv.addObject("resourceTypes", ResourceType.values());
		mv.addObject("resourceTargets", ResourceTarget.values());

		if (null != resource.getParent() && null != resource.getParent().getId()) {
			Map<String, Object> criteria = new HashMap<String, Object>();
			criteria.put("id", resource.getParent().getId());
			Resource parent = resourceService.findEntity(criteria);
			resource.setParent(parent);
		}
		mv.addObject(Keys.KEY_PARAM, resource);

		return mv;
	}

	@RequestMapping("/edit")
	public ModelAndView edit(HttpServletRequest request, ModelAndView mv, Resource resource) {
		if (null == resource || null == resource.getId()) {
			throw new UnifyException(SystemErrorNo.DATA_NOT_EXIST);
		}

		mv.setViewName(Views.EDIT_VIEW);

		Map<String, Object> criteria = new HashMap<String, Object>();
		criteria.put("id", resource.getId());
		resource = resourceService.findEntity(criteria);
		if (null == resource || null == resource.getId()) {
			throw new UnifyException(SystemErrorNo.DATA_NOT_EXIST);
		}

		mv.addObject(Keys.KEY_PARAM, resource);
		mv.addObject("resourceTypes", ResourceType.values());
		mv.addObject("resourceTargets", ResourceTarget.values());
		return mv;
	}

	@ResponseBody
	@RequestMapping("/edit/exec")
	public Result<?> doEdit(HttpServletRequest request, Resource resource) {
		Result<?> result = new Result<String>();
		try {
			// TODO： 数据校验

			resourceService.persistentEntity(resource);

			result.setResultCode(Constants.Common.DEFAULT_RESULT_CODE);
		} catch (ServiceException e) {
			result.setResultCode(e.getErrorNo());
		}
		return result;
	}

	@ResponseBody
	@RequestMapping("/delete/exec")
	public Result<?> doDelete(HttpServletRequest request, Resource resource) {
		Result<?> result = new Result<String>();
		try {
			Map<String, Object> criteria = new HashMap<String, Object>();
			criteria.put("id", resource.getId());
			resourceService.deleteEntity(criteria);
			result.setResultCode(Constants.Common.DEFAULT_RESULT_CODE);
		} catch (ServiceException e) {
			result.setResultCode(e.getErrorNo());
		}
		return result;
	}

	class Views {

		/**
		 * index视图
		 */
		public static final String INDEX_VIEW = "/resource/list";

		/**
		 * 列表视图
		 */
		public static final String LIST_VIEW = "/resource/list";

		/**
		 * 添加视图
		 */
		public static final String ADD_VIEW = "/resource/edit";

		/**
		 * 编辑视图
		 */
		public static final String EDIT_VIEW = "/resource/edit";

	}

}